package com.itheima.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.itheima.bean.Student;
import com.itheima.bean.User;
import com.itheima.core.BaseServlet;
import com.itheima.core.ResponseBody;
import com.itheima.service.impl.StudentServiceImpl;
import com.itheima.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {

    /**
     * 功能：更新学生
     * 该接口接收number,name,birthday,address四个参数,更新成功返回true,否则返回false
     * 测试地址:http://localhost:8080/虚拟路径/user/update?number=heima00X&name=jack&birthday=2020-09-09&address=淖马村
     */
    @ResponseBody
    public Object update(HttpServletRequest request, HttpServletResponse response) {
        try {

            /*1.获取表单数据*/
            Map<String, String[]> map = request.getParameterMap();
            //封装Student对象
            Student stu = new Student();
            //注册日期转换器方法
            dateConvert();
            BeanUtils.populate(stu, map);
            System.out.println("====================="+stu);
            /*2.调用业务层的添加方法*/
            new StudentServiceImpl().updateStu(stu);

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 功能:用户登陆
     * 该接口接收username,password两个参数,如果登陆成功返回true，否则返回false
     * 测试:http://localhost:8080/虚拟路径/user/login?username=admin&password=123
     */
    @ResponseBody
    public Object login(HttpServletRequest request, HttpServletResponse response) {
        try {
            //1.获取表单数据
            String username = request.getParameter("username");
            String password = request.getParameter("password");

            //2.调用Service
            User user = new UserServiceImpl().login(username, password);
            if (user != null) {
                request.getSession().setAttribute("user", user);
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 功能：分页查询学生列表
     * 该接口接收startNum,pageSize两个参数,返回当前页需要的数据
     * 测试地址:http://localhost:8080/虚拟路径/user/pageQuery?pageNum=2&pageSize=4
     */
    @ResponseBody
    public Object pageQuery(HttpServletRequest request, HttpServletResponse response) {
        try {
            Thread.sleep(300);
            /*1.获取表单数据*/
            String pageNum = request.getParameter("pageNum");
            String pageSize = request.getParameter("pageSize");

            if (StringUtil.isEmpty(pageNum)) {
                pageNum = "1";
            }
            if (StringUtil.isEmpty(pageSize)) {
                pageSize = "4";
            }

            /*2.调用service分页查询*/
            PageInfo<Student> pageInfo = new StudentServiceImpl().findByPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));

            return pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 功能：删除学生
     * 该接口接收number 1个参数,删除成功返回true,否则返回false
     * 测试地址:http://localhost:8080/虚拟路径/user/delete?number=heima00X
     */
    @ResponseBody
    public Object delete(HttpServletRequest request, HttpServletResponse response) {
        try {

            /*1.获取表单数据*/
            String number = request.getParameter("number");

            /*2.调用业务层的添加方法*/
            new StudentServiceImpl().deleteStu(number);
            return true;

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    /**
     * 功能：新增学生
     * 该接口接收number,name,birthday,address四个参数,添加成功返回true,否则返回false
     * 测试地址:http://localhost:8080/虚拟路径/user/add?number=heima00X&name=jack&birthday=2020-09-09&address=淖马村
     */
    @ResponseBody
    public Object add(HttpServletRequest request, HttpServletResponse response) {
        try {

            /*1.获取表单数据*/
            Map<String, String[]> map = request.getParameterMap();
            //封装Student对象
            Student stu = new Student();
            //注册日期转换器方法
            dateConvert();
            BeanUtils.populate(stu, map);


            /*2.调用业务层的添加方法*/
            new StudentServiceImpl().addStu(stu);

            return true;
        } catch (Exception e) {
            e.printStackTrace();

            return false;
        }
    }


    /*
       日期转换
    */
    private void dateConvert() {
        ConvertUtils.register(new Converter() {
            public Object convert(Class type, Object value) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    return simpleDateFormat.parse(value.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        }, Date.class);
    }
}
